package car.activities;

import java.util.ArrayList;
import java.util.LinkedList;

import business_layer.UserProfileManager;

import car.manager.BackStageManager;
import car.manager.R;
import car.manager.UserProfile;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;




public class ListOfUsersActivity extends Activity{


	LinkedList <UserProfile> list_of_users;
	private ArrayList<TextView> profile_views;
	private UserProfileManager userProfileManager;
	private LinearLayout linear_layout;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.list_of_users);

		userProfileManager = ((BackStageManager) this.getApplication()).getUserProfileManager();

		if(userProfileManager!=null){
			loadListOfUsers();
		}else{
			showNoProfilesYetMessage();
		}


	}

	private void loadListOfUsers() {

		//list_of_users = (LinkedList<UserProfile>) UserProfile.getAllProfiles();

		list_of_users = (LinkedList<UserProfile>) userProfileManager.getAllUsers();
		try{

			linear_layout= (LinearLayout) findViewById(R.id.loadUsersLayout);
			//		TableLayout table_layout= (TableLayout) findViewById(R.id.LayoutloadUsers);

			if(list_of_users!=null&&!list_of_users.isEmpty()){

				profile_views= new ArrayList<TextView>();

				int index=0;
				for(UserProfile up : list_of_users){
					profile_views.add(index,new TextView(this));
					profile_views.get(index).setClickable(true);
					profile_views.get(index).setTextSize(40);
					profile_views.get(index).setOnClickListener(new View.OnClickListener() {

						public void onClick(View v) {

							int i = profile_views.indexOf(v);
							loadProfile(list_of_users.get(i).getId());

						}


					});

					profile_views.get(index).setText(up.getName().toString());
					linear_layout.addView(profile_views.get(index));
					//				table_layout.addView(profile_views.get(index));

					index++;
				}


			}else{
				showNoProfilesYetMessage();
			}



		}catch(Exception e){
			Log.d(getLocalClassName(),"boooooooomm!!   "+e.toString());
		}


	}

	//an option to add new contact should be added
	private void showNoProfilesYetMessage() {

		TextView tv =	new TextView(this);
		tv.setText("no profiles yet");
		linear_layout.addView(tv);

	}

	private void loadProfile(int id) {

		userProfileManager.setCurrentUserId(id);
		
		//UserProfile.setCurrentUserProfile(id);

		Intent start_profile_manager_intent = new Intent(this, ProfileManagerActivity.class);
		startActivity(start_profile_manager_intent);


	}
	private class PofileClicked implements View.OnClickListener{

		public void onClick(View v) {


		}


	}


}
